package com.itboye.mengwu.app.ui.mymeng;

import java.util.Observable;
import java.util.Observer;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.login.ui.TimeCount;
import com.itboye.mengwu.app.ui.main.MainPresenter;
import com.itboye.mengwu.data.presenter.GetYanzhengMaPresenter;
import com.itboye.mengwu.data.presenter.LiShiTiXianPresenter;

public class ActivityBindZhifubao extends BaseActivity implements Observer {
	TextView txt_title;
	EditText zhifubao_num, renzheng_name, yanzhengma;
	Button find_password_bt;
	TextView huoqu, bindtelphone;
	ImageView back;
	ImageView img_other;

	private Button txtHuoYanZhengMa;

	private TimeCount count;

	private LiShiTiXianPresenter presenter;

	private String uid;
	private String mobile;
	private String account;

	GetYanzhengMaPresenter getYanzhengMaPresenter;
	String code = 6 + "";
	MainPresenter mainPresenter;
	protected String newPwd;
	String queren;
	String realname;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_bind_zhifubao;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub

		if (SPUtils.get(this, null, SpPublicParams.ALIPAY, "").equals("")) {
			bindtelphone.setText("未绑定");
			txt_title.setText("绑定支付宝");
		} else {
			txt_title.setText("更改支付宝");
			bindtelphone.setText("当前支付宝："
					+ SPUtils.get(this, null, SpPublicParams.ALIPAY, "") + "("
					+ SPUtils.get(this, null, SpPublicParams.realname, "")
					+ ")");
		}
		uid = SPUtils.get(ActivityBindZhifubao.this, null, SpPublicParams.UID,
				"") + "";
		mobile = SPUtils.get(ActivityBindZhifubao.this, null,
				SpPublicParams.PHONE_MODEL, "") + "";
		account = zhifubao_num.getText().toString();
		realname = renzheng_name.getText().toString();
		count = new TimeCount(60000, 1000, txtHuoYanZhengMa);
		getData();
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		txtHuoYanZhengMa.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (zhifubao_num.getText() != null)
					mobile = zhifubao_num.getText().toString().trim();
				// presenter.getBangDing(uid, mobile, code, alipay);
				getYanzhengMaPresenter.getVerificationCode(mobile, "6");
				count.start();
			}
		});
		find_password_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (account == null) {
					ByAlert.alert("请输入绑定支付宝用户姓名");

				} else {
					showProgressDialog("绑定中,请稍后", true);
					find_password_bt.setEnabled(false);
					code = yanzhengma.getText().toString();
					account = zhifubao_num.getText().toString();
					realname = renzheng_name.getText().toString();
					// ByAlert.alert(""+alipay);
					presenter.getBangDing(uid, mobile, code, account, realname);
				}
			}
		});
	}

	private void getData() {
		mainPresenter = new MainPresenter(this);
		presenter = new LiShiTiXianPresenter(this);
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == LiShiTiXianPresenter.BangDing_success) {
				ByAlert.alert(entity.getData() + "");
				finish();
			}
			if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaSuccessEvent) {
				code = entity.getData().toString();
				ByAlert.alert(code);
				yanzhengma.setText("");

			}

		}
		find_password_bt.setEnabled(true);
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

}
